package cm.epet.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import cm.epet.ConnectionHelper;
import cm.epet.dao.Interface.IPetStoreDAO;
import cm.epet.model.Pet;
import cm.epet.model.PetOwner;

public class PetStoreDAO implements IPetStoreDAO{
	public double breed(PetOwner po,int id,String name,String type,double price) throws SQLException {
		Connection con = null;
		// TODO Auto-generated method stub
		try {
			con = ConnectionHelper.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Statement stmt = con.createStatement();
		ResultSet rs = null;
		double result = 0;
		rs = stmt.executeQuery("select id from pet where id='"+id+"'");
		if(rs.next()) {
			result=1;
			return result;
		}
		rs = stmt.executeQuery("select name from pet where name='"+name+"'");
		if(rs.next()) {
			result=2;
			return result;
		}
		stmt.executeUpdate("insert into pet (id,name,type,price,owner_name) values('"+id+"','"+name+"','"+type+"','"+price+"','"+po.getName()+"')");
		rs.close();
		return result;
	}
	public double charge(Pet pet, PetOwner po,double price) throws SQLException {
		Connection con = null;
		// TODO Auto-generated method stub
		try {
			con = ConnectionHelper.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Statement stmt = con.createStatement();
		ResultSet rs = null;
		double result;
		rs = stmt.executeQuery("select name from pet where name='"+pet.getName()+"' and owner_name='"+po.getName()+"' ");
		if(rs.next()) {
			stmt.executeUpdate("update pet set price='"+price+"' where name='"+pet.getName()+"'");
			result=1;
		}
		else {
			result=2;
		}
		rs.close();
		return result;
	}
	public ArrayList<String> getAccount(PetOwner po) throws SQLException {
		Connection con = null;
		// TODO Auto-generated method stub
		try {
			con = ConnectionHelper.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Statement stmt = con.createStatement();
		ResultSet rs = null;
		ArrayList<String> info = new ArrayList<String>();
		rs = stmt.executeQuery("select * from account where seller_name='"+po.getName()+"' or buyer_name='"+po.getName()+"'");
		while(rs.next()) {
			info.add(rs.getString(1));
			info.add(rs.getString(2));
			info.add(rs.getString(3));
			info.add(rs.getString(4));
			info.add(rs.getString(5));
			info.add(rs.getString(6));
			info.add(rs.getString(7));
			}
		rs.close();
		return info;
	}
	public PetOwner login(String name,String password) throws SQLException {
		Connection con = null;
		// TODO Auto-generated method stub
		try {
			con = ConnectionHelper.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Statement stmt = con.createStatement();
		ResultSet rs = null;
		PetOwner po = null;
			stmt.execute("select * from petstore where name='"+name+"' and password='"+password+"'");
			rs = stmt.getResultSet();
			if(rs.next()) {
				po = new PetOwner(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getDouble(4));
				
			}
		return po;
	}
	public ArrayList<String> getAllStore() throws SQLException {
		Connection con = null;
		// TODO Auto-generated method stub
		try {
			con = ConnectionHelper.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Statement stmt = con.createStatement();
		ResultSet rs = null;
		ArrayList<String> info = new ArrayList<String>();
		rs = stmt.executeQuery("select * from petstore");
		while(rs.next())
		{
			info.add(rs.getString(1));
			info.add(rs.getString(2));
		}
		return info;
	}
	@Override
	public ArrayList<String> displayMyPet(PetOwner po) throws SQLException {
		// TODO Auto-generated method stub
		Connection con = null;
		// TODO Auto-generated method stub
		try {
			con = ConnectionHelper.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ArrayList<String> info = new ArrayList<String>();
		Statement stmt = con.createStatement();
		ResultSet rs = null;
		rs = stmt.executeQuery("select id,name from pet where owner_name='"+po.getName()+"'");
		if(rs.next()) {
			rs.beforeFirst();
			while(rs.next()) {
				info.add(rs.getString(1));
				info.add(rs.getString(2));
		}
		}
		else {
			info=null;
		}
		rs.close();
		return info;
	}
}
